﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Administration;

namespace Winwise.SPWebConfig.Core.CodeModification
{
    public class CodeWebConfigModification : SPWebConfig.Core.WebConfigModification
    {
        [Persisted]
        private string m_AssemblyName;

        [Persisted]
        private string m_TypeName;

        public override void ApplyModification(System.Xml.XmlDocument doc, Microsoft.SharePoint.Administration.SPUrlZone zone, Microsoft.SharePoint.Administration.SPWebApplication webapp)
        {

            IWebConfigModificationHandler handler = null;
            try
            {
                handler = GetHandler();
                handler.ApplyWebConfigModification(doc, zone, webapp);
            }
            catch (Exception e)
            {
                string message = string.Format("Error Applying Web Config modifications. handler:{0}, zone:{1}, webapp:{2}, Error:{3}",
                       handler == null ? "NULL" : handler.GetType().ToString(),
                       zone.ToString(),
                       webapp.Name,
                       e.ToString());
                throw new Exception(message);
            }
        }

        public override void RemoveModification(System.Xml.XmlDocument doc, Microsoft.SharePoint.Administration.SPUrlZone zone, Microsoft.SharePoint.Administration.SPWebApplication webapp)
        {
            IWebConfigModificationHandler handler = null;
            try
            {
                handler = GetHandler();
                handler.UnApplyWebConfigModification(doc, zone, webapp);
            }
            catch (Exception e)
            {
                string message = string.Format("Error UnApplying Web Config modifications. handler:{0}, zone:{1}, webapp:{2}, Error:{3}",
                       handler == null ? "NULL" : handler.GetType().ToString(),
                       zone.ToString(),
                       webapp.Name,
                       e.ToString());
                throw new Exception(message);
            }
        }

        public string AssemblyName
        {
            get
            {
                return m_AssemblyName;
            }
            set
            {
                m_AssemblyName = value;
            }
        }

        public string TypeName
        {
            get
            {
                return m_TypeName;
            }
            set
            {
                m_TypeName = value;
            }
        }

        public IWebConfigModificationHandler GetHandler()
        {
            var o = Activator.CreateInstance(AssemblyName, TypeName);
            if (o == null)
            {
                string message = String.Format("Custom WebConfig modif : cannot instantitate class with AssemblyName : {0} and Type : {1}",
                    AssemblyName, TypeName);
                throw new Exception(message);
            }
            IWebConfigModificationHandler app = o.Unwrap() as IWebConfigModificationHandler;
            if (app == null)
            {
                string message = String.Format("Custom WebConfig modif : cannot instantitate class with AssemblyName : {0} and Type : {1}",
                   AssemblyName, TypeName);
                throw new Exception(message);
            }
            return app;
        }

        public override string FriendlyTypeName
        {
            get { return "Code Modification"; }
        }

        public override string AdminPage
        {
            get { return "Winwise.SPWebConfig\\AddEditCodeWebconfigModification.aspx"; }
        }

        public override bool SupportEditing
        {
            get {return true; }
        }

        public override void Validate()
        {
            GetHandler();
        }

    }
}
